/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exemplo1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author G0006185
 */
public class ContactTable extends AbstractTableModel{
    
    private String[] colunas=new String[]{"id","Nome", "email", "Nascimento","Endereco"};

    private List<ContatoDto> lista;

    public ContactTable(List<ContatoDto> contato) {
        this.lista.addAll(contato);;
    }
     public ContactTable() {
      lista =new ArrayList();
    }
    @Override
    public int getRowCount() {
        return lista.size();
    }

    @Override
    public int getColumnCount() {
        return colunas.length;
    }

    @Override
    public Object getValueAt(int rowindex, int columnIndex) {
        ContatoDto customer = lista.get(rowindex);
         SimpleDateFormat dtp = new SimpleDateFormat("dd/MM/yyyy");
  
        switch (columnIndex) {
            case 0: return customer.getId();
            case 1: return customer.getNome();
            case 2: return customer.getEmail();
            case 3: return dtp.format(customer.getDataNascimento().getTime());
            case 4: return customer.getEndereco();
            default: return null;
        }   
    }
   /* @Override
    public void setValueAt(Object value, int row, int col) {
        ContatoDto contato = lista.get(row);
        try{
            switch (col) {
                case 0:
                    contato.setId((Long) value); //if column 0 (code)
                    break;
                case 1:
                    contato.setNome((String) value);
                    break;
                case 2:
                    contato.setEmail((String) value);
                    break;
                case 3:
                    Calendar cal = Calendar.getInstance();
                    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                    Date data = format.parse((String) value);
                    cal.setTime(data);
                    contato.setDataNascimento(cal);
                    break;
                case 4:
                    contato.setEndereco((String) value);
                    break;
                default:
            }
            this.fireTableCellUpdated(row, col);
        }catch(ParseException ex){
            ex.printStackTrace();
        }
    }*/
     public void setListaContatos(List<ContatoDto> contatos) {
        this.lista = contatos;
        this.fireTableDataChanged();
        //this.fireTableRowsInserted(0,contatos.size()-1);//update JTable
    }
    
}
